//
// main.cpp for nibbler in /home/lefebv_w//workspace/cpp/nibbler/v2
// 
// Made by damien lefebvre
// Login   <lefebv_w@epitech.net>
// 
// Started on  Fri Mar  8 12:46:53 2013 damien lefebvre
// Last update Fri Jul 26 17:39:52 2013 mickael gomes
//

#include	<iostream>
#include	<string>
#include	<dlfcn.h>
#include	<sstream>
#include	<stdlib.h>

#include	"header.hh"
#include	"IGraphicLibrary.hh"
#include	"Game.hh"
#include	"Case.hh"

static bool	load_library(char const *path, _Handler *handle, maker_IGraphicLibrary *pMaker)
{
  const char		*dlsym_error;

  *handle = dlopen(path, RTLD_LAZY);
  if (!(*handle))
    {
      std::cerr << "Error : " << dlerror() << std::endl;
      return (true);
    }
  *pMaker = (maker_IGraphicLibrary)(dlsym(*handle, "make_IGraphicLibrary"));
  if ((dlsym_error = dlerror()))
    {
      std::cerr << "Error : " << dlsym_error << std::endl;
      return (true);
    }
  return (false);
}

int		main(int ac, char **av)
{
  unsigned int	       	size_x;
  unsigned int	       	size_y;
  _Handler		handle;
  std::stringstream    	strStream1;
  std::stringstream    	strStream2;
  
  maker_IGraphicLibrary	pMaker;

  srandom(time(NULL) * getpid());
  if (ac < 4)
    {
      std::cerr << "[USAGE] : ./nibbler size_x size_y ./graphic_library" << std::endl;
      return (EXIT_FAILURE);
    }
  strStream1 << av[1];
  strStream1 >> size_x;
  strStream2 << av[2];
  strStream2 >> size_y;
  if (size_x < MIN_SIZE_X || size_x > MAX_SIZE_X || size_y < MIN_SIZE_Y || size_y > MAX_SIZE_Y)
    {
      std::cerr << "Error : invalid size. Must be between " << MIN_SIZE_X << "and " << MAX_SIZE_X << "." << std::endl;
      return (EXIT_FAILURE);
    }
  if (load_library(av[3], &handle, &pMaker))
    {
      std::cerr << "Cannot load library." << std::endl;
      return (EXIT_FAILURE);
    }
  std::vector<IGraphicLibrary::InfoPicture> list;
  list.push_back(IGraphicLibrary::InfoPicture("data/empty1.png", ' ', IGraphicLibrary::IMG_EMPTY_1));
  list.push_back(IGraphicLibrary::InfoPicture("data/empty2.png", ' ', IGraphicLibrary::IMG_EMPTY_2));
  list.push_back(IGraphicLibrary::InfoPicture("data/empty3.png", ' ', IGraphicLibrary::IMG_EMPTY_3));
  list.push_back(IGraphicLibrary::InfoPicture("data/empty4.png", ' ', IGraphicLibrary::IMG_EMPTY_4));
  list.push_back(IGraphicLibrary::InfoPicture("data/wall.png", '#', IGraphicLibrary::IMG_WALL));
  list.push_back(IGraphicLibrary::InfoPicture("data/snake_part1.png", 'O', IGraphicLibrary::IMG_SNAKE_PART2));
  list.push_back(IGraphicLibrary::InfoPicture("data/snake_part2.png", 'O', IGraphicLibrary::IMG_SNAKE_PART1));
  list.push_back(IGraphicLibrary::InfoPicture("data/foot.png", 'o', IGraphicLibrary::IMG_SNAKEFOOT));
  list.push_back(IGraphicLibrary::InfoPicture("data/partbis.png", 'P', IGraphicLibrary::IMG_BONUSPART));
  list.push_back(IGraphicLibrary::InfoPicture("data/head.png", '0', IGraphicLibrary::IMG_SNAKEHEAD));
  list.push_back(IGraphicLibrary::InfoPicture("data/bonus5_1.png", '5', IGraphicLibrary::IMG_BONUS5PART_1));
  list.push_back(IGraphicLibrary::InfoPicture("data/bonus5_2.png", ' ',IGraphicLibrary::IMG_BONUS5PART_2));
  list.push_back(IGraphicLibrary::InfoPicture("data/bonuslife.png", 'L', IGraphicLibrary::IMG_BONUSLIFE));

  IGraphicLibrary	*graph;
  graph = pMaker(size_x * SIZE_CASE, size_y * SIZE_CASE + PX_SIZE_FOOTER, list);
 
  Game		game(*graph, size_x, size_y);

  game.play();
  dlclose(handle);

  return (EXIT_SUCCESS);
}
